package zhaomh.catgirlcharm.module;

import zhaomh.zhlib.ZhLib;
import zhaomh.zhlib.event.EventTarget;
import zhaomh.zhlib.event.impl.EventChat;
import zhaomh.zhlib.modpacks.functionMod.ToggleModule;
import zhaomh.zhlib.util.TextUtil;

import static zhaomh.catgirlcharm.CatGirlCharm.MOD_ID;

public class ChatModule extends ToggleModule {
    public ChatModule() {
        super(MOD_ID, "function", "catgirlchat");
        ZhLib.getEventManager().register(this);
    }

    @Override
    public void configChange() {

    }

    @EventTarget
    public void chat(EventChat e) {
        if (getConfig().getValue()) {
            e.setMessage(toCatGirlMsg(e.getMessage(), TextUtil.Translatable("catgirlcharm.config.function.catgirlchat.meow").getString()));
        }
    }

    private String toCatGirlMsg(String msg, String meow) {

        StringBuilder sb = new StringBuilder();
        boolean b = false;
        for (int i = 0; i < msg.length(); i++) {
            char ch = msg.charAt(i);
            if ("!！?？,，.。~".indexOf(ch) != -1) {
                if (!b) {
                    sb.append(meow);
                }
                b = true;
            } else {
                b = false;
            }
            sb.append(ch);
        }

        if (!b) {
            sb.append(meow);
        }

        return sb.toString();
    }
}
